#include "qenumpropertyeditor.h"

#include "../property/qabstractproperty.h"

#include <QHBoxLayout>

QEnumPropertyEditor::QEnumPropertyEditor(QAbstractProperty *property,QWidget *parent):
    QAbstractPropertyEditor(property,parent),
    m_combobox(new QComboBox)
{
    layout()->addWidget(m_combobox);
    m_items=m_property->get_attribute("items").value<ComboItems>();
    foreach(tagComboItem item,m_items)
    {
        m_combobox->addItem(QIcon(item.m_icon),item.m_text);
        if(property->get_value()==item.m_value)
        {
            m_combobox->setCurrentIndex(m_items.indexOf(item));
        }
    }

    connect(m_combobox,SIGNAL(currentIndexChanged(int)),this,SLOT(value_changed(int)));
}

void QEnumPropertyEditor::value_changed(int index)
{
    tagComboItem item=m_items.value(index);
    if(item.m_value!=m_property->get_value())
    {
        emit property_changed(item.m_value);
    }
}

void QEnumPropertyEditor::property_value_changed()
{
    foreach(tagComboItem item,m_items)
    {
        if(m_property->get_value()==item.m_value)
        {
            m_combobox->setCurrentIndex(m_items.indexOf(item));
            return;
        }
    }
}

qRegisterPropertyEditor("Enum",QEnumPropertyEditor)
